import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import http from "@/utils/http";
import moment from "moment/moment";
import ElementUI from "element-ui";
import "./utils/flexible";
import "./element-variables.scss";
import "./assets/font/iconfont.css";
Vue.config.productionTip = false;
import { getToken, getUserInfo, setUserInfo } from "@/utils/public";
import throttle from "@/utils/public";
import JsonViewer from "vue-json-viewer";
import VueCookies from "vue-cookies";

Vue.directive("throttle", throttle);
Vue.use(JsonViewer);
Vue.use(ElementUI, {
  size: "small",
});
Vue.use(VueCookies);
// 挂载全局
Vue.prototype.$http = http;
Vue.prototype.$moment = moment;
Vue.prototype.CHANNEL_TYPE = {
  wechat: "微信",
  ali: "支付宝",
  ada: "汇付",
  yse: "银盛",
  fuiou: "富友",
  douyin: "抖音",
  balance: "余额",
};

Vue.prototype.$backTo = function (path) {
  router.push({ path });
  // if (window.history.length <= 2) {
  //   console.log(111)
  //   this.$router.push({ path });
  // } else {
  //   console.log(222)
  //   this.$router.back(-1);
  // }
};
Vue.prototype.$backToAlive = function (path) {
  // console.log(store)
  store.dispatch("changeLive", false);
  setTimeout(() => {
    store.dispatch("changeLive", true);
  }, 0);
  router.push({ path });
};

Vue.prototype.$flexColumnWidth = function (column, dataList) {
  if (!column) {
    return;
  }
  let maxlength = 95; //在此处设置默认宽度
  if (column == "column1") {
    //在此处为了保证表头不换行，可以根据表头名称长度设置默认宽度
    maxlength = 120;
  }
  for (let i = 0; i < dataList.length; i++) {
    if (dataList[i][column]) {
      let now_temp = dataList[i][column] + "";
      let flexWidth = 0;
      for (const char of now_temp) {
        if ((char >= "A" && char <= "Z") || (char >= "a" && char <= "z")) {
          //英文字母 8 像素
          flexWidth += 8;
        } else if (char >= "\u4e00" && char <= "\u9fa5") {
          //中文文字 15 像素
          flexWidth += 15;
        } else {
          //其他字符 10 像素
          flexWidth += 10;
        }
      }
      if (flexWidth > maxlength) {
        maxlength = flexWidth;
      }
    }
  }
  //el-table中 cell 有左右的 padding 个 10 像素
  return maxlength + 20 + "px";
};

// 获取商户，用户注册信息
Vue.prototype.$getMechantUserRegisInfo = function () {
  let user = getUserInfo();
  // 信息没有完善 必须完善信息
  if (user.merchantInfoComplete != "success") {
    return http.post("/sys/user/refreshCurrentUser").then((res) => {
      if (res.code === "0000") {
        console.log(res.data);
        setUserInfo({ ...res.data });
        return res.data.merchantInfoComplete; //返回注册状态
      }
    });
  } else {
    return "success";
  }
};

//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
  const user = getToken();
  if(to.path=='/loginMobile'||to.path=='/qiyeUserCreate'){
    next()
  }else{
  if (!user && to.path !== "/login" && to.path !== "/mobile") {
    next("/login");
  } else {
    if (to.path !== "/login") {
      // const menu = store.state.merchantMenu.length;
      if (!user) {
        next("/login");
      } else {
        next();
      }
    } else {
      next();
    }
  }

    // // 简单的判断IE10及以下不进入富文本编辑器，该组件不兼容
    // if (navigator.userAgent.indexOf('MSIE') > -1 && to.path === '/editor') {
    //   Vue.prototype.$alert('vue-quill-editor组件不兼容IE10及以下浏览器，请使用更高版本的浏览器查看', '浏览器不兼容通知', {
    //     confirmButtonText: '确定'
    //   });
    // } else {
    //   if (from.path === '/login' && from.query !== undefined
    //       && from.query.redirect !== undefined
    //       && from.query.redirect !== to.path) {
    //         next(from.query.redirect);
    //   } else {
    //       next();
    //   }
    // }
  }
});
new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");
